<template>
  <div class="login">

    <div class="login-body">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">凌云后台管理系统</h3>
        <el-form-item prop="userName">
          <el-input v-model="loginForm.userName" type="text" auto-complete="off" placeholder="账号">
            <i slot="prefix" class="el-icon-user"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
          >
            <i slot="prefix" class="el-icon-lock"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="verificationCode" v-if="captchaOnOff">
          <el-input
              v-model="loginForm.verificationCode"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
          >
            <i slot="prefix" class="el-icon-circle-check"/>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>

          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>



    </div>


    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2021-2022 meishibiexuejava.cn All Rights Reserved.</span>
    </div>

  </div>
</template>

<script>

import {getCodeImg} from "@/api/admin/login";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        userName: "2418870649@qq.com",
        password: "nacos",
        verificationCode: "",
        keyCode: ""
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      // 验证
      loginRules: {
        userName: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        verificationCode: [
          {required: true, trigger: "change", message: "请输入验证码"}
        ]
      },

    }


  },
  methods: {
    // 获取图形验证码
    getCode() {
      getCodeImg().then(res => {
        if (this.captchaOnOff) {
          this.codeUrl = res.image;
          this.loginForm.keyCode = res.keyCode;
        }
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login',this.loginForm).then(()=>{
            this.$router.push({path: "/admin"}).catch(() => {});
          }).catch(() => {
            this.loading = false;
            this.getCode();
          });
        }
      })
    }
  },

  watch: {
    $route: {
      handler: function (route) {
        console.log("当前路径")
        console.log(route)
      },
      immediate: true
    }
  },

  created() {
    this.getCode()
  }

};
</script>

<style scoped>
.login {
  height: 100%;
}

.login-body{
  width: 100%;
  height: 100%;
  background-image: url("../assets/beijing.jpg");
  background-size: cover;
  margin: 0 auto;
  padding: 220px 0 144px 0;
  position: relative;
}
.login-form {
  border-radius: 6px;
  padding: 25px 25px 5px 25px;
  background: #fff;
  min-width: 260px;
  width: 368px;
  margin: 0 auto;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.login-form .el-input {
  height: 38px;
}
.login-form .el-input input {
  height: 38px;
}
.login-form .input-icon {
  height: 39px;
  width: 14px;
  margin-left: 2px;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
}
.login-code img {
  cursor: pointer;
  vertical-align: middle;
}

.login-code-img {
  height: 38px;
}





.el-login-footer {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 0 16px;
  margin: 48px 0 24px;
  text-align: center;




  height: 40px;
  line-height: 40px;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
}

</style>
